import 'package:flutter/material.dart';

import '../models/calendar_event.dart';

/// 日历事件列表组件
class CalendarEventList extends StatelessWidget {
  /// 要显示的事件列表
  final List<CalendarEvent> events;
  /// 点击事件的回调函数
  final Function(CalendarEvent) onEventTap;
  /// 空状态消息
  final String emptyStateMessage;

  /// 构造函数
  const CalendarEventList({
    Key? key,
    required this.events,
    required this.onEventTap,
    this.emptyStateMessage = '没有事件',
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 如果没有事件，显示空状态
    if (events.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              Icons.event_busy,
              size: 64,
              color: Colors.grey,
            ),
            const SizedBox(height: 16),
            Text(
              emptyStateMessage,
              style: const TextStyle(color: Colors.grey),
            ),
          ],
        ),
      );
    }

    // 按时间排序事件
    final sortedEvents = List<CalendarEvent>.from(events)
      ..sort((a, b) => a.startTime.compareTo(b.startTime));

    return ListView.builder(
      padding: const EdgeInsets.all(8),
      itemCount: sortedEvents.length,
      itemBuilder: (context, index) {
        final event = sortedEvents[index];
        return _buildEventCard(context, event);
      },
    );
  }

  /// 构建事件卡片
  Widget _buildEventCard(BuildContext context, CalendarEvent event) {
    return Card(
      elevation: 2,
      margin: const EdgeInsets.symmetric(vertical: 8),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: InkWell(
        onTap: () => onEventTap(event),
        borderRadius: BorderRadius.circular(12),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 事件类型图标
              CircleAvatar(
                backgroundColor: event.eventType.color,
                child: IconTheme(
                  data: const IconThemeData(color: Colors.white),
                  child: event.eventType.icon,
                ),
              ),
              const SizedBox(width: 16),
              // 事件详情
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      event.title,
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                    const SizedBox(height: 4),
                    Text(
                      event.description,
                      style: Theme.of(context).textTheme.bodyMedium,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 8),
                    // 事件时间和地点信息
                    Row(
                      children: [
                        const Icon(
                          Icons.access_time,
                          size: 16,
                          color: Colors.grey,
                        ),
                        const SizedBox(width: 4),
                        Text(
                          _formatEventTime(event),
                          style: Theme.of(context).textTheme.bodySmall,
                        ),
                        if (event.location != null) ...[  
                          const SizedBox(width: 16),
                          const Icon(
                            Icons.location_on,
                            size: 16,
                            color: Colors.grey,
                          ),
                          const SizedBox(width: 4),
                          Expanded(
                            child: Text(
                              event.location!,
                              style: Theme.of(context).textTheme.bodySmall,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 格式化事件时间
  String _formatEventTime(CalendarEvent event) {
    if (event.isAllDay) {
      return '全天';
    }
    
    final startTime = '${event.startTime.hour.toString().padLeft(2, '0')}:${event.startTime.minute.toString().padLeft(2, '0')}';
    
    if (event.endTime != null) {
      final endTime = '${event.endTime!.hour.toString().padLeft(2, '0')}:${event.endTime!.minute.toString().padLeft(2, '0')}';
      return '$startTime - $endTime';
    }
    
    return startTime;
  }
}
